package com.cylise.coolweather.util;

import android.content.Context;
import android.database.Cursor;
import android.util.Log;
import android.widget.Toast;

import com.cylise.coolweather.R;
import com.cylise.coolweather.db.City;
import com.cylise.coolweather.db.Country;
import com.cylise.coolweather.db.Province;
import com.cylise.coolweather.db.Town;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.litepal.crud.DataSupport;

import java.io.IOException;
import java.util.HashSet;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

/**
 * Created by Cylise on 2017/3/28.
 */

public class DbUtil{

    public static void InitDB (String url){

        HttpUtil.sendOKHttpRequest(url, new Callback() {
           @Override
           public void onFailure(Call call, IOException e) {
               e.printStackTrace();

           }

           @Override
           public void onResponse(Call call, Response response) throws IOException {
               String body = response.body().string();
               Gson gson=new Gson();
               List<Town> towns;
               towns = gson.fromJson(body,new TypeToken<List<Town>>(){}.getType());
               if (!towns.isEmpty()){
                   Log.d("town","GetTown");
                   DataSupport.saveAll(towns);

                   /*获取省信息*/
                   Log.d("town","GetPro");
                   String sql="select provinceZh from Town where countryZh='中国' group by provinceZh";
                   Cursor cursor= DataSupport.findBySQL(sql);
                   cursor.moveToFirst();
                   while (cursor.moveToNext()){
                       Province province = new Province();
                       province.setName(cursor.getString(0));
                       province.save();
                   }
                   cursor.close();



                   /*获取地市信息*/
                   Log.d("town","GetCity");
                   sql="select provinceZh,leaderZh from Town where countryZh='中国' group by provinceZh,leaderZh";
                   cursor= DataSupport.findBySQL(sql);
                   cursor.moveToFirst();
                   while (cursor.moveToNext()){
                       City city = new City();
                       city.setProvince(cursor.getString(0));
                       city.setName(cursor.getString(1));
                       city.save();
                   }
                   cursor.close();

                   /*获取县信息*/
                   Log.d("town","GetCountry");
                   sql="select cnId,leaderZh,cityZh from Town where countryZh='中国'";
                   cursor= DataSupport.findBySQL(sql);
                   cursor.moveToFirst();
                   while (cursor.moveToNext()){
                       Country country = new Country();
                       country.setWeather_id(cursor.getString(0));
                       country.setName(cursor.getString(2));
                       country.setCity(cursor.getString(1));
                       country.save();
                   }
                   cursor.close();
               }


           }

       });
   }


}
